<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="客户名称，地址"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-on:click="getCustomers">
            <i class="el-icon-search"></i>
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-plus"></i>
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-table :data="customerList" style="width: 100%" stripe>
      <el-table-column type="index" min-width="100">
      </el-table-column>
      <el-table-column prop="customername" label="客户名" min-width="280">
      </el-table-column>
      <el-table-column prop="createdat" label="创建时间">
      </el-table-column>
      <el-table-column prop="updatedat" label="更新时间">
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑客户" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form label-width="100px" :model="editForm" :rules="editFormRules" ref="editForm">

        <el-form-item label="用户名" width="100%">
          <el-input v-model="editForm.customername"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--新增界面-->
    <el-dialog title="新增客户" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form label-width="100px" :model="addForm" :rules="addFormRules" ref="addForm">

        <el-form-item label="用户名" width="100%">
          <el-input v-model="addForm.customername"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
  <script>
import NProgress from "nprogress";
export default {
  data() {
    return {
      customerList: [],
      filters: {
        name: ""
      },
      total: 0,
      page: 1,

      editFormVisible: false, //编辑界面是否显示
      editLoading: false,
      //编辑界面数据
      editForm: {
        customerid: null,
        customername: "",
      },
      editFormRules: {
        customername: {
          required: true,
          message: "请输入客户名称",
          trigger: "blur"
        }
      },
      addFormVisible: false, //编辑界面是否显示
      addLoading: false,
      //新增界面数据
      addForm: {
        customerid: null,
        customername: "",
      },
      addFormRules: {
        customername: {
          required: true,
          message: "请输入客户名称",
          trigger: "blur"
        }
      }
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.getCustomers();
    },
    //获取客户列表
    getCustomers() {
      this.listLoading = true;
      NProgress.start();
      this.axios
        .get("/customers", {
          params: {
            page: this.page,
            pagesize: 10,
            customername: this.filters.name
          }
        })
        .then(reply => {
          this.listLoading = false;
          NProgress.done();
          if (reply.data.status === 0) {
            this.total = reply.data.data.total;
            this.customerList = reply.data.data.list;
          } else {
            this.$message({
              message: `查询出错7，错误码${reply.data.status},请联系管理员`,
              type: "warning"
            });
          }
        });
    },

    //删除
    handleDel: function(index, row) {
      this.$confirm("确认删除该客户吗?", "提示", {
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        NProgress.start();
        this.axios
          .post("/customers/delete ", {
            customerid: row.customerid
          })
          .then(reply => {
            this.listLoading = false;
            NProgress.done();
            if (reply.data.status === 0) {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success"
              });
              this.getCustomers();
            } else {
              this.$message({
                message: `删除失败，错误码${reply.data.status},请联系管理员`,
                type: "warning"
              });
            }
          });
      });
    },

    //显示编辑界面
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign(this.editForm, row);
    },
    //编辑
    editSubmit: function() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            NProgress.start();
            let params = {};
            Object.assign(params, this.editForm);
            this.axios.post("/customers/update", params).then(reply => {
              this.editLoading = false;
              NProgress.done();
              if (reply.data.status === 0) {
                this.$notify({
                  title: "成功",
                  message: "编辑成功",
                  type: "success"
                });
                this.$refs["editForm"].resetFields();
                this.editFormVisible = false;
                this.getCustomers();
              } else {
                this.$message({
                  message: `保存失败，错误码${reply.data.status},请联系管理员`,
                  type: "warning"
                });
              }
            });
          });
        }
      });
    },
    //显示新增界面
    handleAdd: function() {
      this.addFormVisible = true;
      this.addForm = {
        customerid: null,
        customername: "",
      }
    },
    //新增
    addSubmit: function() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.addLoading = true;
            NProgress.start();
            let params = {};
            Object.assign(params, this.addForm);
            this.axios.post("/customers/insert", params).then(reply => {
              this.addLoading = false;
              NProgress.done();
              this.$refs["addForm"].resetFields();
              this.addFormVisible = false;
              this.getCustomers();
              if (reply.data.status === 0) {
                this.$notify({
                  title: "成功",
                  message: "新增成功",
                  type: "success"
                });
              } else {
                console.log(JSON.stringify(reply.data));
                this.$message({
                  message: `保存出错，错误码${reply.data.status},请联系管理员`,
                  type: "warning"
                });
              }
            });
          });
        }
      });
    }
  },
  mounted() {
    this.getCustomers();
  }
};
</script>